const tools = require("./pub/tools");
class Action{
    constructor(){
        this._data = {};
    }

    do( meth  ){
        let scope = this;
        let args = Array.prototype.slice.call(arguments);
        let newArgs = args.splice(1);
        let me = this;
        let p = new Promise( (resolve, reject )=>{
            let arr= [resolve , reject];
            if( typeof meth == 'function'){
                return meth.apply( scope , arr.concat( newArgs ) )
            }else if( me[meth] ){
                return me[meth].apply( scope , arr.concat(newArgs) )
            }else if( me._data[meth] ){
                return  me._data[meth].handler ?
                        me._data[meth].handler.apply( me._data[meth] , arr.concat(newArgs) ) :
                        me._data[meth].apply( scope , arr.concat(newArgs) );
            }else{
                resolve( true );
                reject('cant find meth:' + meth);
            }
        });
        return p;
    }

    appendActions( p ){
        p = p || {};
        for(let key in p){
           !this._data[key] && ( this._data[key] = p[key] );
        }
    }
}
module.exports = Action;